package com.cmpe.group6.storyroad.ListAdapters;

import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.cmpe.group6.storyroad.R;

/** List adapter for comments list.
 *
 */
public class CommentAdapter extends BaseAdapter {
	Context context;
	List<String> commentUsers;
	List<String> commentBodies;
	List<String> commentDates;

	/**
	 * @param context Current context.
	 * @param users Owners of the comments.
	 * @param bodies Bodies of the comments.
	 * @param dates Dates of the comments.
	 */
	public CommentAdapter(Context context, List<String> users,
			List<String> bodies, List<String> dates) {
		this.context = context;
		this.commentUsers = users;
		this.commentBodies = bodies;
		this.commentDates = dates;
	}

	/** ViewHolder for quicker generation.
	 *
	 */
	private class ViewHolder {

		TextView username;
		TextView commentBody;
		TextView date;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return this.commentBodies.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return this.commentBodies.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return 0;
	}

	/* (non-Javadoc)
	 * @see android.widget.Adapter#getView(int, android.view.View, android.view.ViewGroup)
	 */
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder = null;

		LayoutInflater mInflater = (LayoutInflater) context
				.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
		if (convertView == null) {
			convertView = mInflater.inflate(R.layout.comment_item, null);
			holder = new ViewHolder();
			holder.username = (TextView) convertView
					.findViewById(R.id.comment_user);
			holder.commentBody = (TextView) convertView
					.findViewById(R.id.comment_body);
			holder.date = (TextView) convertView
					.findViewById(R.id.comment_date);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}

		String usernameValue = this.commentUsers.get(position);
		String commentValue = this.commentBodies.get(position);
		String dateValue = this.commentDates.get(position);

		holder.commentBody.setText(commentValue);
		holder.username.setText(usernameValue + " says: ");
		holder.date.setText(dateValue);

		return convertView;
	}

}
